package com.winbomb.kingcard.screens.util;

import java.util.List;

import android.graphics.Color;
import android.graphics.Paint;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.GameMsg;
import com.winbomb.kingcard.IAttackRoute;
import com.winbomb.kingcard.IBattle;
import com.winbomb.kingcard.ICard;
import com.winbomb.kingcard.IForce;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.IPlayerInfo;
import com.winbomb.kingcard.IState;
import com.winbomb.kingcard.Pos;
import com.winbomb.kingcard.Suit;
import com.winbomb.kingcard.base.BattleState;
import com.winbomb.kingcard.base.Card;
import com.winbomb.kingcard.base.Force;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.base.GameState;
import com.winbomb.kingcard.base.PlayerInfo;
import com.winbomb.kingcard.framework.Graphics;
import com.winbomb.kingcard.utils.GameUtils;

public class Render {

	private static final long NUMBERS[] = { 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L };

	/** 四种花色的位置 */
	private static final int[] SUIT_POS = new int[] { 101, 121, 141, 161 };

	/** poker width:扑克牌的宽度 */
	public static final int PK_W = 49;
	/** 扑克牌的高度 */
	public static final int PK_H = 69;
	/** 扑克牌堆叠时的空隙 */
	public static final int PK_I = 30;

	/** 头像的宽度和高度 */
	public static final int PORT_W = 57;
	public static final int PORT_H = 45;

	/** 提示语的行高 */
	private static final int TIPS_H = 20;

	/** 战场上各个位置的坐标 , 0表示下方的坐标,1表示上方的坐标 */
	private static final Pos[] GENERAL_POS = new Pos[] { new Pos(20, 263), new Pos(414, 16) };
	private static final Pos[] FIRST_HAND_POS = new Pos[] { new Pos(24, 245), new Pos(418, 60) }; // 先后手的位置
	private static final Pos[] CARD_COUNT_POS = new Pos[] { new Pos(73, 264), new Pos(373, 23) };
	private static final Pos[] LEFT_POINT_POS = new Pos[] { new Pos(112, 297), new Pos(353, 35) };
	private static final Pos[] FLAG_POS = new Pos[] { new Pos(382, 261), new Pos(14, 44) };
	private static final Pos[] CARDS_POS = new Pos[] { new Pos(86, 182), new Pos(86, 68) };

	private static Graphics g;
	private IGameLogic game;

	/** 下面是一些预定义的画刷 */
	public static final Paint YELLOW_PAINT = new Paint();
	public static final Paint WHITE_PAINT = new Paint();
	public static final Paint BLACK_PAINT = new Paint();
	public static final Paint RED_PAINT = new Paint();
	public static final Paint BLUE_PAINT = new Paint();
	public static final Paint BONUS_PAINT = new Paint();

	public Render(IGameLogic game, Graphics gParam) {
		g = gParam;
		this.game = game;

		YELLOW_PAINT.setColor(Color.YELLOW);
		WHITE_PAINT.setColor(Color.WHITE);
		BLACK_PAINT.setColor(Color.BLACK);
		RED_PAINT.setColor(Color.RED);
		BLUE_PAINT.setColor(Color.BLUE);

		BONUS_PAINT.setColor(Color.YELLOW);
		BONUS_PAINT.setTextSize(14);
	}

	public Graphics getGraphics() {
		return g;
	}

	/**
	 * 绘制玩家
	 * 
	 * @param player
	 */
	public static void drawPlayer(IPlayer player) {

		IPlayerInfo playerInfo = player.getPlayerInfo();

		// 绘制背景
		Pos framePos = playerInfo.getFramePos();
		g.drawPixmap(Assets.player_frame, framePos.x, framePos.y, 0, 0, 80, 60);

		// 绘制头像
		g.drawPixmap(Assets.head_icon, framePos.x + 2, framePos.y + 2, 46 * player.getPlayerIndex(), 0, 46, 41);

		Pos campPos = playerInfo.getCampPos();

		// 玩家姓名和得分
		g.drawText(player.getPlayerName(), framePos.x + 2, framePos.y + 55, YELLOW_PAINT);
		g.drawText(player.getStatsInfo().getScore(), framePos.x + 50, framePos.y + 56, YELLOW_PAINT);

		// 剩余牌数
		int cardCnt = player.getCardCount() + player.getBattleCards().size();
		g.drawText(cardCnt, framePos.x + 56, framePos.y + 41);

		// 绘制营寨
		if (!player.isAlive()) {
			g.drawPixmap(Assets.camps, campPos.x, campPos.y, 208, 0, 16, 16);
		} else if (player.isDefender()) {
			g.drawPixmap(Assets.camps, campPos.x, campPos.y, 224, 0, 16, 16);
		} else if (player.isAttacker()) {
			g.drawPixmap(Assets.camps, campPos.x, campPos.y, 240, 0, 16, 16);
		} else {
			int flag = player.getForce().getForceFlag();
			boolean isMaster = (player.getMaster() == player);
			g.drawPixmap(Assets.camps, campPos.x, campPos.y, 16 * flag, (isMaster) ? 0 : 16, 16, 16);
		}

		// 势力标志
		if (player.getForce() != null) {
			int flag = player.getForce().getForceFlag();
			g.drawPixmap(Assets.flags, framePos.x + 49, framePos.y + 3, flag * 23, 0, 23, 26);

			// 同盟标志
			flag = player.getForce().getAleinFlag();
			if (flag != -1) {
				g.drawPixmap(Assets.flags, framePos.x + 30, framePos.y + 3, 0, 26, 18, 14);
			}
		}

		// 是否在受攻击
		if (player.isDefender()) {
			g.drawPixmap(Assets.select_frame, framePos.x, framePos.y, 75, 0, 75, 58);
		}

		// 是否已经灭亡
		if (!player.isAlive()) {
			g.drawPixmap(Assets.defeated_mark, framePos.x, framePos.y);
		}
	}

	/**
	 * 绘制游戏状态信息
	 */
	public void drawInfoBoard() {

		// g.drawText("STATES: " + game.getState(), 100, 20);

		// 游戏进行时间
		float gameTime = game.getTimeCost();
		StringBuffer sb = new StringBuffer();
		int min = (int) (gameTime / 60);
		int sec = ((int) gameTime) % 60;
		if (min < 10) {
			sb.append("0");
		}
		sb.append(min);
		sb.append(":");
		if (sec < 10) {
			sb.append("0");
		}
		sb.append(sec);
		g.drawText(sb.toString(), 137, 93, YELLOW_PAINT);

		// 剩余势力数:
		int masterCnt = GameUtils.getMasterCount(game.getPlayers());
		g.drawText(String.valueOf(masterCnt), 147, 133, YELLOW_PAINT);

		// 剩余牌数
		int cardCnt = game.getCardPile().getTotalCardCount();
		g.drawText(String.valueOf(cardCnt), 147, 113, YELLOW_PAINT);

		// 回合数
		int roundCnt = game.getRoundCount();
		g.drawText(String.valueOf(roundCnt), 147, 153, YELLOW_PAINT);
	}

	/**
	 * 绘制消息
	 * 
	 * @param msgs
	 */
	public void drawMessageBoard(GameMsg[] msgs) {

		int y0 = 96;
		for (int i = 0; i < msgs.length; i++) {
			if (msgs[i] == null) {
				continue;
			}

			int msgBodyCount = msgs[i].getMsgBodyCount();
			Paint paint = null;
			for (int j = 0, x0 = 178; j < msgBodyCount; j++) {
				switch (msgs[i].getMsgColor(j)) {
				case Color.YELLOW:
					paint = YELLOW_PAINT;
					break;
				case Color.RED:
					paint = RED_PAINT;
					break;
				case Color.BLUE:
					paint = BLUE_PAINT;
					break;
				default:
					paint = WHITE_PAINT;
				}

				g.drawText(msgs[i].getMsgBody(j), x0, y0, paint);
				x0 += paint.measureText(msgs[i].getMsgBody(j));
			}
			y0 += 14;
		}
	}

	public void drawForceInfo() {
		int count = 0;
		StringBuffer sb;
		for (IPlayer player : game.getPlayers()) {
			if (player.getMaster() == player) {
				IForce force = player.getForce();
				sb = new StringBuffer();
				sb.append(player.getPlayerName());
				sb.append(",");
				sb.append(force.getCardCount());
				sb.append(",");
				sb.append(force.getMemeberCount());

				int x = (count < 6) ? 178 : 280;
				int y = (count < 6) ? 96 + 12 * count : 96 + 12 * (count - 6);
				g.drawText(sb.toString(), x, y);
				count++;
			}
		}
	}

	/**
	 * 绘制战败后的告示板
	 * 
	 */
	public void drawDefeatedBoard() {
		g.drawPixmap(Assets.defeated, 0, 0);
	}

	/**
	 * 绘制胜利后的告示板
	 */
	public void drawVictoryBorad(IPlayer winner) {
		g.drawPixmap(Assets.victory, 0, 0);
		g.drawText("最后的胜利者是: " + winner, 100, 100);
	}

	/**
	 * 绘制进攻路线
	 */
	public void drawAttackRoute() {
		IAttackRoute atkRoute = game.getCurrTurn().getAttackRoute();
		IPlayer[] players = atkRoute.getAttackRoute();
		boolean isClockwise = atkRoute.isAtkClockwise();
		for (int i = 0; i < players.length - 1; i++) {
			int srcSeatNum = players[i].getPlayerInfo().getSeatNum();
			int destSeatNum = players[i + 1].getPlayerInfo().getSeatNum();

			int tgSeatNum = GameUtils.getNextSeatNum(srcSeatNum, isClockwise);
			do {
				Pos srcPos = PlayerInfo.SEAT_POS[srcSeatNum];
				Pos tgPos = PlayerInfo.SEAT_POS[tgSeatNum];
				g.drawLine(srcPos.x + 8, srcPos.y + 8, tgPos.x + 8, tgPos.y + 8, Color.RED);

				srcSeatNum = tgSeatNum;
				tgSeatNum = GameUtils.getNextSeatNum(srcSeatNum, isClockwise);
			} while (srcSeatNum != destSeatNum);
		}

		IPlayer attacker = players[0];
		IPlayer target = players[players.length - 1];
		for (int i = 1; i < players.length - 1; i++) {
			if (!players[i].isAlive()) {
				continue;
			}

			Pos pos = players[i].getPlayerInfo().getCampPos();
			if (GameUtils.isSameForce(attacker, players[i])
					|| attacker.getForce().hasRelation(players[i].getForce(), Force.RT_ALEIN)) {
				g.drawPixmap(Assets.camps, pos.x, pos.y, 208, 16, 16, 16);

			} else if (GameUtils.isSameForce(target, players[i])
					|| target.getForce().hasRelation(players[i].getForce(), Force.RT_ALEIN)) {
				g.drawPixmap(Assets.camps, pos.x, pos.y, 192, 16, 16, 16);

			} else {
				g.drawPixmap(Assets.camps, pos.x, pos.y, 224, 16, 16, 16);
			}
		}
	}

	/**
	 * 绘制提示语（这里的提示语是指游戏中固有的一些提示语）
	 * 
	 * @param tipId
	 *            提示语的ID
	 * @param x
	 * @param y
	 */
	public void drawTips(int tipId, int x, int y) {
		int tipY = tipId * TIPS_H;
		g.drawPixmap(Assets.tips, x, y, 0, tipY, 250, TIPS_H);
	}

	public void drawTips() {
		IState gameState = game.getState();
		int x = 130, y = 295;
		if (gameState == GameState.SELECT_TARGET) {
			drawTips(0, x, y);
		} else if (gameState == GameState.SELECT_ATK_CARDS) {
			drawTips(1, x, y);
		} else if (gameState == GameState.SELECT_DEF_CARDS) {
			drawTips(2, x, y);
		} else if (gameState == GameState.SELECT_CHANGE_CARD) {
			drawTips(3, x, y);
		} else if (gameState == GameState.SELECT_SENDOUT_TARGET) {
			drawTips(4, x, y);
		} else if (gameState == GameState.SELECT_SENDOUT_CARDS) {
			drawTips(5, x, y);
		} else if (gameState == GameState.SELECT_CALLUP_SOURCE) {
			drawTips(6, x, y);
		} else if (gameState == BattleState.CHASE_AND_ATTACK) {
			drawTips(7, x, y);
		} else if (gameState == BattleState.SELECT_CHASE_CARDS) {
			drawTips(8, x, y);
		}
	}

	/**
	 * 绘制数字
	 * 
	 * @param number
	 * @param x
	 * @param y
	 */
	public void drawNumber(int number, int x, int y) {

		boolean bNegtive = (number < 0);
		if (bNegtive) {
			g.drawPixmap(Assets.number, x, y, 120, 0, 12, 12);
			x += 12;
		}

		int digitCount = 1;
		int test = (number >= 0) ? number : 0 - number;
		for (int i = 0; i < NUMBERS.length; i++) {
			if (test < NUMBERS[i]) {
				break;
			}
			digitCount++;
		}

		for (int i = 0; i < digitCount; i++) {
			int digit = number % 10;
			number = number / 10;
			g.drawPixmap(Assets.number, x + (digitCount - i - 1) * 12, y, digit * 12, 0, 12, 12);
		}
	}

	/**
	 * 绘制战斗的过程
	 */
	public static void drawBattleField(IGameLogic game) {
		IBattle battle = game.getBattle();

		// 背景
		g.drawPixmap(Assets.battle_field, 10, 10);

		// 绘制牌
		drawPlayerArmy(game, battle.getInvader(), battle.getInvadeCards());
		drawPlayerArmy(game, battle.getGuarder(), battle.getGuardCards());

		IState state = game.getState();

		// 绘制按钮和提示
		if ((state == BattleState.INVADER_SET_GENERAL || state == BattleState.GUARDER_SET_GENERAL)
				&& game.getCurrPlayer().isHuman()) {
			ICard[] atkCards = battle.getAtkCards();
			boolean hasGeneral = false;
			for (int i = 0; i < atkCards.length; i++) {
				if (atkCards[i] != null && atkCards[i].testFlag(Card.IS_GENERAL)) {
					hasGeneral = true;
					g.drawPixmap(Assets.marks, CARDS_POS[0].x + 64 * i, CARDS_POS[0].y, 0, 0, 50, 70);
				}
			}

			g.drawPixmap(Assets.battle_elems, 154, 261, hasGeneral ? 0 : 71, 79, 71, 36);
			g.drawPixmap(Assets.battle_elems, 250, 261, 0, 117, 71, 36);
			g.drawPixmap(Assets.battle_tips, 140, 142, 0, 0, 200, 30);

		} else if (state == BattleState.OFFENSIVE_ARRAY || state == BattleState.DEFENSIVE_ARRAY) {

			// 排兵布阵
			g.drawPixmap(Assets.battle_elems, 201, 261, 50, 40, 78, 37);
			if (game.getCurrPlayer().isHuman()) {
				g.drawPixmap(Assets.battle_tips, 140, 142, 0, 30, 200, 30);
			}

		} else if (state == BattleState.SELECT_ATK_CARD && game.getCurrPlayer().isHuman()) {
			g.drawPixmap(Assets.battle_tips, 140, 142, 0, 60, 200, 30);

		} else if (state == BattleState.BATTLE_END) {
			g.drawPixmap(Assets.battle_elems, 201, 261, 50, 40, 78, 37);
		}

		// 绘制胜败
		if (state == BattleState.BATTLE_END) {
			boolean isDownWin = battle.getWinner().isHuman();

			// 胜利 & 失败
			g.drawPixmap(Assets.anim_victory_defeated, 135, (isDownWin) ? 155 : 60, 1150, 0, 230, 106);
			g.drawPixmap(Assets.anim_victory_defeated, 135, (isDownWin) ? 60 : 155, 1150, 106, 230, 106);

			// 胜败级别
			int winLevel = battle.getWinLevel();
			int loseLevel = battle.getLoseLevel();
			g.drawPixmap(Assets.marks, 315, (isDownWin) ? 225 : 125, winLevel * 39, 71, 39, 16);
			g.drawPixmap(Assets.marks, 315, (isDownWin) ? 125 : 225, loseLevel * 39, 87, 39, 16);
		}
	}

	public static void drawBattleCards(IPlayer player) {

		// 绘制牌
		List<ICard> battleCards = player.getBattleCards();

		for (int i = 0; i < battleCards.size(); i++) {
			if (battleCards.get(i) == null || battleCards.get(i).testFlag(Card.IS_DEFEATED)) {
				continue;
			}
			int x = 42 + i * 43;
			int y = (battleCards.get(i).isSelected()) ? 244 : 248;
			drawCard(battleCards.get(i), x, y);
		}
	}

	// public static void drawChaseCards(IBattle battle) {
	//
	// // 绘制背景
	// g.drawPixmap(Assets.chase_field, 10, 10);
	//
	// // 绘制牌
	// ICard[] winnerCards = battle.getWinnerCards();
	// ICard[] captiveCards = battle.getWinner().getCaptiveCards();
	//
	// int sltCnt = 0;
	// int leftPoint = 0;
	// for (int i = 0; i < winnerCards.length; i++) {
	// if (winnerCards[i] == null || winnerCards[i].testFlag(Card.IS_DEFEATED))
	// {
	// continue;
	// }
	// int x = 23 + i * 64;
	// int y = 126;
	// drawCard(winnerCards[i], x, y);
	// if (winnerCards[i].isSelected()) {
	// g.drawPixmap(Assets.marks, x, y, 0, 0, 50, 70);
	// sltCnt++;
	// leftPoint += winnerCards[i].getPoint();
	// }
	// }
	//
	// for (int i = 0; i < captiveCards.length; i++) {
	// if (captiveCards[i] == null) {
	// continue;
	// }
	// int x = 343 + i * 64;
	// int y = 126;
	// drawCard(captiveCards[i], x, y);
	// g.drawPixmap(Assets.marks, x + 23, y + 2, 199, 71, 24, 42);
	// if (captiveCards[i].isSelected()) {
	// g.drawPixmap(Assets.marks, x, y, 0, 0, 50, 70);
	// sltCnt++;
	// leftPoint += captiveCards[i].getPoint();
	// }
	// }
	//
	// // 绘制选中的个数和点数
	// g.drawPixmap(Assets.card_count_number, CARD_COUNT_POS[0].x,
	// CARD_COUNT_POS[0].y, 34 * sltCnt, 0, 34, 34);
	// g.drawText(leftPoint, LEFT_POINT_POS[0].x, LEFT_POINT_POS[0].y,
	// WHITE_PAINT);
	//
	// // 绘制按钮
	// if (sltCnt < 1 || sltCnt > 5) {
	// g.drawPixmap(Assets.battle_elems, 145, 220, 93, 203, 83, 32);
	// } else {
	// g.drawPixmap(Assets.battle_elems, 145, 220, 3, 203, 83, 32);
	// }
	//
	// g.drawPixmap(Assets.battle_elems, 250, 220, 4, 239, 83, 32);
	// }

	private static void drawPlayerArmy(IGameLogic game, IPlayer player, ICard[] cards) {

		IBattle battle = game.getBattle();
		boolean isInvader = (battle.getInvader() == player);
		boolean onUpside = (!battle.getInvader().isHuman()) ? isInvader : !isInvader;
		int idx = (onUpside) ? 1 : 0;

		// 绘制用户姓名
		g.drawText(player.getPlayerName(), (onUpside) ? 50 : 350, (onUpside) ? 32 : 295, WHITE_PAINT);

		// 绘制剩余部队数量
		int leftCardCnt = GameUtils.getLiveCardCount(cards);
		if (leftCardCnt >= 0 && leftCardCnt <= GameLogic.MAX_PLAY_CARD) {
			g.drawPixmap(Assets.card_count_number, CARD_COUNT_POS[idx].x, CARD_COUNT_POS[idx].y, 34 * leftCardCnt, 0,
					34, 34);
		} else {
			g.drawPixmap(Assets.card_count_number, CARD_COUNT_POS[idx].x, CARD_COUNT_POS[idx].y, 204, 0, 34, 34);
		}

		// 绘制剩余点数
		if (!player.isHuman()) {
			// 所有牌都亮开了,才能绘制点数
			boolean isAllOpen = true;
			for (ICard card : cards) {
				if (card != null && card.testFlag(Card.IS_UNOPEN_CARD)) {
					isAllOpen = false;
					break;
				}
			}

			if (!isAllOpen) {
				g.drawText("??", LEFT_POINT_POS[idx].x, LEFT_POINT_POS[idx].y, WHITE_PAINT);
			} else {
				int leftPoint = GameUtils.getLeftCardPoint(cards);
				g.drawText(leftPoint, LEFT_POINT_POS[idx].x, LEFT_POINT_POS[idx].y, WHITE_PAINT);
			}

		} else {
			int leftPoint = GameUtils.getLeftCardPoint(cards);
			g.drawText(leftPoint, LEFT_POINT_POS[idx].x, LEFT_POINT_POS[idx].y, WHITE_PAINT);
		}

		// 绘制主将
		// ICard general = (player == battle.getInvader()) ?
		// battle.getInvadeGeneral() : battle.getGuardGeneral();
		// if (general != null) {
		// drawCard(general, GENERAL_POS[idx].x, GENERAL_POS[idx].y, true);
		// }

		// 绘制头像
		g.drawPixmap(Assets.head_icon, GENERAL_POS[idx].x, GENERAL_POS[idx].y, player.getPlayerIndex() * 46, 0, 46, 41);

		// 绘制先后手
		if (player == battle.getFirstHandPlayer()) {
			g.drawPixmap(Assets.marks, FIRST_HAND_POS[idx].x, FIRST_HAND_POS[idx].y, 198, 71, 38, 16);
		} else if (battle.getFirstHandPlayer() != null) {
			g.drawPixmap(Assets.marks, FIRST_HAND_POS[idx].x, FIRST_HAND_POS[idx].y, 198, 87, 38, 16);
		}

		// 绘制是否到自己的回合
		// if (battle.getCurrTurner() == player) {
		// g.drawPixmap(Assets.select_frame, GENERAL_POS[idx].x,
		// GENERAL_POS[idx].y, 2, 2, 46, 41);
		// }

		// 在选将阶段,标记出能够作为将的牌.
		boolean needMarkOut = (player.isHuman() && (game.getState() == BattleState.INVADER_SET_GENERAL || game
				.getState() == BattleState.GUARDER_SET_GENERAL));

		// 绘制战旗和卡片
		for (int i = 0; i < GameLogic.MAX_PLAY_CARD; i++) {
			if (cards[i] == null) {
				continue;
			}

			// 绘制战旗
			int flagX = FLAG_POS[idx].x + i * 17;
			int srcX = (!cards[i].testFlag(Card.IS_DEFEATED)) ? 0 : 16;
			g.drawPixmap(Assets.battle_elems, flagX, FLAG_POS[idx].y, srcX, 0, 16, 16);

			// 绘制卡片
			int x = CARDS_POS[idx].x + 64 * i;
			int y = CARDS_POS[idx].y;
			drawCard(cards[i], x, y);

			// 绘制技能
			// 只绘制human自己的鼓舞技能,不然可以通过对方牌的鼓舞标志看出敌牌的花色
			if (player.isHuman() || !cards[i].testFlag(Card.IS_UNOPEN_CARD)) {

				// 鼓舞
				boolean isCheeredUp = cards[i].hasSkill(Card.SKILL_CHEER_UP)
						&& (player.isHuman() || !cards[i].testFlag(Card.IS_UNOPEN_CARD));
				g.drawPixmap(Assets.battle_elems, x + 30, CARDS_POS[idx].y + 5, 0, (isCheeredUp) ? 32 : 16, 16, 16);

				// 连诛
				g.drawPixmap(Assets.battle_elems, x + 30, CARDS_POS[idx].y + 23, 16, cards[i]
						.hasSkill(Card.SKILL_ROW_KILLER) ? 32 : 16, 16, 16);

				// 空城
				g.drawPixmap(Assets.battle_elems, x + 30, CARDS_POS[idx].y + 41, 32, cards[i]
						.hasSkill(Card.SKILL_EMPTY_CITY) ? 32 : 16, 16, 16);

			}

			// 绘制卡片状态
			if (cards[i].testFlag(Card.IS_DEFEATED)) {
				g.drawPixmap(Assets.marks, x, y, 150, 0, 50, 70);
			} else if (!cards[i].testFlag(Card.IS_UNOPEN_CARD)) {
				g.drawPixmap(Assets.marks, x, y, 200, 0, 50, 70);
			}

			if (cards[i].testFlag(Card.IS_ATK_CARD)) {
				g.drawPixmap(Assets.marks, x, y, 0, 0, 50, 70);
			}

			if (cards[i].testFlag(Card.IS_FIRST_CHOICE)) {
				g.drawPixmap(Assets.marks, x, y, 50, 0, 50, 70);
			} else if (cards[i].testFlag(Card.IS_RECOMMENDED)) {
				g.drawPixmap(Assets.marks, x, y, 250, 0, 50, 70);
			} else if (cards[i].testFlag(Card.UNDER_ATTACK)) {
				g.drawPixmap(Assets.marks, x, y, 300, 0, 50, 70);
			}

			// 在选将阶段,标记出能够作为将的牌.
			if (needMarkOut && (cards[i].getPoint() >= 11 && cards[i].getPoint() <= 15)) {
				g.drawPixmap(Assets.marks, x, y, 300, 0, 50, 70);
			}
		}

	}

	/**
	 * 绘制战斗结果
	 */
	public void drawBattleResult() {
		IBattle battle = game.getBattle();

		// 背景
		g.drawPixmap(Assets.battle_field, 10, 10);
		g.drawText("游戏状态:" + game.getState(), 20, 30);
		g.drawText("胜者:" + battle.getWinner(), 20, 45);

		// 战败的卡片
		ICard[] defeatedCards = battle.getDefeatedCards();
		for (int i = 0; i < defeatedCards.length; i++) {
			int x = (i < 5) ? 45 + 85 * i : 45 + 85 * (i - 5);
			int y = (i < 5) ? 75 : 180;

			drawCard(defeatedCards[i], x, y);
		}

	}

	/**
	 * 绘制扑克（正常尺寸）
	 * 
	 * @param card
	 * @param x
	 * @param y
	 * @param bFront
	 *            是否是正面
	 */
	public static void drawCard(ICard card, int x, int y) {
		drawCard(card, x, y, false);
	}

	public static void drawCard(ICard card, int x, int y, boolean ignoreCovered) {

		if (card.isCovered() && !ignoreCovered) {
			g.drawPixmap(Assets.poker, x, y, 49, 0, PK_W, PK_H);
			return;
		}

		// 绘制牌面
		g.drawPixmap(Assets.poker, x, y, 0, 0, PK_W, PK_H);

		// 大小王
		if (card.isBlackJoker() || card.isRedJoker()) {
			g.drawPixmap(Assets.poker, x + 5, y + 5, (card.isRedJoker()) ? 101 : 115, 0, 14, 42);
			g.drawPixmap(Assets.poker, x + 15, y + 15, (card.isRedJoker()) ? 128 : 168, 0, 40, 45);
			return;
		}

		// 绘制点数和花色
		Suit suit = card.getSuit();

		// 绘制点数
		int x0, y0;
		x0 = 20 * (13 - card.getPoint());
		y0 = (Suit.SPADE == suit || Suit.CLUB == suit) ? 71 : 91;
		g.drawPixmap(Assets.poker, x + 2, y + 2, x0, y0, 20, 20);

		// 绘制花色
		int suitPos = SUIT_POS[suit.ordinal()];
		g.drawPixmap(Assets.poker, x + 2, y + 25, suitPos, 50, 20, 20);

		// 绘制兵种
		String kind = "未知";
		if (card.isBlackJoker() || card.isRedJoker()) {
			kind = "王";
		} else if (card.getPoint() == 1 || card.getPoint() == 2) {
			kind = "士";
		} else if (card.getPoint() >= 11 && card.getPoint() <= 13) {
			kind = "将";
		} else if (card.getPoint() >= 3 && card.getPoint() <= 10) {
			kind = "兵";
		}
		g.drawText(kind, x + 4, y + 64);
	}

	/**
	 * 绘制牌的背面
	 * 
	 * @param x
	 * @param y
	 */
	public static void drawCardBack(int x, int y) {
		g.drawPixmap(Assets.poker, x, y, 50, 0, PK_W, PK_H);
	}

	/**
	 * 绘制交涉的界面
	 */
	public void drawNegotiateBoard() {

		// 绘制背景
		g.drawPixmap(Assets.negotiate, 80, 60);

		g.drawText(game.getCurrTurn().getPlayerName(), 120, 94, WHITE_PAINT);
	}
}
